package ar.edu.itba.ia2010.geneticos.motor.api.sintaxistree.bitwise;

import java.util.Random;

import org.apache.commons.lang.Validate;

import ar.edu.itba.ia2010.geneticos.motor.api.sintaxistree.Expresion;
import ar.edu.itba.ia2010.geneticos.motor.api.sintaxistree.ExpresionGenerator;
import ar.edu.itba.ia2010.geneticos.motor.api.sintaxistree.impl.ExpresionPrimitive;

/**
 * Crea {@link Variable}s.
 *  
 * @author Juan F. Codagnone
 * @since Mar 28, 2010
 */
public class VariableExpresionPrimitive 
     extends AbstractTerminalBinaryExpresionPrimitive  {
    private final Expresion[] e;

    /** Creates the VariableExpresionFactory. */
    public VariableExpresionPrimitive(final int nVariables) {
        Validate.isTrue(nVariables > 0);
        e = new Expresion[nVariables];
        for(int i = 0; i < nVariables; i++) {
            e[i] = new Variable(i);
        }
    }
    
    /** @see ExpresionPrimitive#create(Random, int, ExpresionGenerator) */
    public final Expresion create(final Random random, final int maxAltura,
            final ExpresionGenerator expresionGenerator) {
        return e[random.nextInt(e.length)];
    }
    
    /** @see ExpresionPrimitive#canCreate(char) */
    public final boolean canCreate(final char c) {
        return c >= 'a' && c <= 'z';
    }

};